package com.kuaimacode.kframework.admin.controller.content;


import com.github.pagehelper.PageInfo;
import com.kuaimacode.kframework.admin.services.content.BootstrapImgService;
import com.kuaimacode.kframework.admin.services.sys.SysUploadService;
import com.kuaimacode.kframework.admin.util.SessionUtil;
import com.kuaimacode.kframework.common.Constant;
import com.kuaimacode.kframework.common.JsonResult;
import com.kuaimacode.kframework.constants.PageConstants;
import com.kuaimacode.kframework.mybatis.models.index.BootstrapImg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

@Controller
@RequestMapping("/bootstrapimg")
public class BootstrapImgController {

    private static final String PAGE_FILE_NAME = "bootstrapimg/";

    @Autowired
    private BootstrapImgService bootstrapImgService;

    @Autowired
    private SysUploadService sysUploadService;

    @RequestMapping("/index")
    public String queryAllImg(@RequestParam(value = "pageNum", required = true, defaultValue = PageConstants.PAGE_NUM+"") int pageNum,
                              @RequestParam(value = "pageSize", required = true, defaultValue = PageConstants.PAGE_SIZE+"") int pageSize,
                              String notes,ModelMap model){



        PageInfo<BootstrapImg> pageInfo = bootstrapImgService.findAll(pageNum, pageSize,notes);
        model.put("pageInfo",pageInfo);
        model.put("notes",notes);

        return PAGE_FILE_NAME +"index";
    }

    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String  add(ModelMap map){
        BootstrapImg bootstrapImg = new BootstrapImg();
        map.put("isAdd",true);
        map.put("imgs",new ArrayList());
        map.put("bootstrapImg",bootstrapImg);
        return PAGE_FILE_NAME+"form";

    }

    @RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
    public String edit(@PathVariable String id, ModelMap map) {
        BootstrapImg bootstrapImg = bootstrapImgService.getById(id);
        map.put("bootstrapImg", bootstrapImg);

        String imgs = bootstrapImg.getImgUrl();
        String[] imgs_path = null;
        if(imgs!=null &&!"".equals(imgs)){
            imgs_path = imgs.split(",");
        }
        map.put("imgs",imgs_path);
        map.put("isAdd", false);

        if(bootstrapImg.getType()==0) return PAGE_FILE_NAME+"start_form";

        return PAGE_FILE_NAME+"form";
    }


    @RequestMapping(value = "/detail/{id}", method = RequestMethod.GET)
    public String detail(@PathVariable String id, ModelMap map) {
        BootstrapImg bootstrapImg = bootstrapImgService.getById(id);
        map.put("bootstrapImg", bootstrapImg);
        String imgs = bootstrapImg.getImgUrl();
        String[] imgs_path = null;
        if(imgs!=null &&!"".equals(imgs)){
            imgs_path = imgs.split(",");
        }
        map.put("imgs",imgs_path);
        map.put("isAdd", false);
        return PAGE_FILE_NAME+"detail";
    }


    @Transactional
    @RequestMapping(value = {"/edit"}, method = RequestMethod.POST)
    @ResponseBody
    public JsonResult edit(BootstrapImg bootstrapImg, Boolean isAdd,ModelMap map) {
        try {
            /*//判断是否立即生效
            if("0".equals(bootstrapImg.getStatus())){
                //立即生效 更新该类型的其他记录为失效
                bootstrapImgService.updateStatus(bootstrapImg.getType().toString(),"1");

            }*/
            //去掉url 的第一个字符
            /*if(bootstrapImg.getImgUrl()!=null&&!"".equals(bootstrapImg.getImgUrl()))
                bootstrapImg.setImgUrl(bootstrapImg.getImgUrl().substring(1));*/
            if(isAdd){
                // 新建保存
                bootstrapImg.setCreateBy(SessionUtil.getUserId());
                bootstrapImg.setCreateAt(new Date());
                bootstrapImgService.save(bootstrapImg);
            }else{
                // 编辑保存
                bootstrapImg.setUpdateAt(new Date());
                bootstrapImg.setUpdateBy(SessionUtil.getUserId());

                bootstrapImgService.updateById(bootstrapImg);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.failure(e.getMessage());
        }
        return JsonResult.success();
    }


    @RequestMapping(value = {"/delete/{id}"}, method = RequestMethod.POST)
    @ResponseBody
    public JsonResult delete(@PathVariable String id, ModelMap map) {
        try {
            bootstrapImgService.removeById(id);
        } catch (Exception e) {
            return JsonResult.failure(e.getMessage());
        }
        return JsonResult.success();
    }



}
